package commands;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import twitter4j.internal.org.json.JSONArray;
import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;
import database.models.TagModel;

/**
 * Command that gets the data for a graphical timeline representation of tweets.
 * 
 * @author Project group 9
 * 
 */
public class GetTimelineTagDataCommand implements Command {

	private JSONObject jsonObject;
	private TagModel tagModel;

	public GetTimelineTagDataCommand(JSONObject jsonObject, TagModel tagModel) {
		this.jsonObject = jsonObject;
		this.tagModel = tagModel;
	}

	@Override
	public String execute() throws JSONException {
		JSONObject sendObject = new JSONObject();

		JSONObject parameters = jsonObject.getJSONObject("getTimelineTagData");
		ArrayList<HashMap<String, int[]>> timeline = tagModel
				.getTimelineTagData(parameters);

		for (HashMap<String, int[]> tag : timeline) {
			Iterator<Map.Entry<String, int[]>> it = tag.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, int[]> entry = it
						.next();
				JSONArray dataArray = new JSONArray();

				for (Integer i : tag.get(entry.getKey())) {
					dataArray.put(i);
				}
				sendObject.put((String) entry.getKey(), dataArray);
			}
		}

		return sendObject.toString();
	}

}
